/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.performer;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringListener;
import com.tumblr.backboard.Utils.Property;
import ohos.agp.components.Component;

/**
 * Directly maps the motion of an Component.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class Performer implements SpringListener {
    /**
     * The view to modify.
     */
    protected Component mTarget;
    /**
     * The property of the view to modify.
     */
    protected Property<Component, Float> mProperty;

    /**
     * Constructor. Note that an {@link Component} must be specified by {@link #setTarget(Component)}.
     *
     * @param property the view property to modify.
     */
    public Performer(Property<Component, Float> property) {
        this(null, property);
    }

    /**
     * Constructor.
     *
     * @param target the view to modify.
     * @param property the view property to modify.
     */
    public Performer(Component target, Property<Component, Float> property) {
        this.mTarget = target;
        this.mProperty = property;
    }

    /**
     * getTarget
     *
     * @return mTarget
     */
    public Component getTarget() {
        return mTarget;
    }

    /**
     * setTarget
     *
     * @param target target
     */
    public void setTarget(Component target) {
        this.mTarget = target;
    }

    /**
     * getProperty
     *
     * @return mProperty
     */
    public Property getProperty() {
        return mProperty;
    }

    /**
     * setProperty
     *
     * @param property property
     */
    public void setProperty(Property<Component, Float> property) {
        this.mProperty = property;
    }

    @Override
    public void onSpringUpdate(Spring spring) {
        if (mProperty != null && mTarget != null) {
            mProperty.set(mTarget, (float) spring.getCurrentValue());
        }
    }

    @Override
    public void onSpringAtRest(Spring spring) {
    }

    @Override
    public void onSpringActivate(Spring spring) {
    }

    @Override
    public void onSpringEndStateChange(Spring spring) {
    }
}
